package cn.didi.feidi.androidorder.fragment.personal;


import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.util.AppUtil;

import static cn.didi.feidi.androidorder.R.style.dialog;


public class PersonInvitedByWaterDeliverFragment extends FeidiBaseFragment {

    private TextView saveWaterDeliver;
    private EditText editWaterDeliver;

    private static final String TAG = "InvitedByWaterDeliver";

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonInvitedByWaterDeliverFragment newInstance(Bundle bundle) {
        PersonInvitedByWaterDeliverFragment fragment = new PersonInvitedByWaterDeliverFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_app_setting_invited_by_water_deliver, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }

    @Override
    protected void initViews(View rootView) {
        editWaterDeliver = (EditText) rootView.findViewById(R.id.app_setting_invited_by_water_deliver_edit);
        saveWaterDeliver = (TextView) rootView.findViewById(R.id.app_setting_invited_by_water_deliver_save);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addListener() {
        saveWaterDeliver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String feidiAccountOrPhone = editWaterDeliver.getText().toString().trim();
                if (AppUtil.checkFeidiAccount(feidiAccountOrPhone) || AppUtil.checkPhone(feidiAccountOrPhone)) {
                    setWaterDeliverInServer(feidiAccountOrPhone);
                } else {
                    showInputFalseDialog("账号格式错误，请重新输入");
                }

            }
        });

    }

    private void showInputFalseDialog(String content) {
        new CommomDialog(getContext(), dialog, content, new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    editWaterDeliver.setText("");
                    dialog.dismiss();
                }
            }
        }, true, true).setPositiveButton("确定", "#347cf1").show();
    }

    private void setWaterDeliverInServer(final String feidiAccount) {

        Map<String, String> data = new HashMap<>();
        data.put("userId", UserManager.getUserId());
        data.put("token", UserManager.getToken());
        data.put("waterDeliver", feidiAccount);
        executeRequest(new AppActionRequest<>(Api.BIND_WATER_DELIVER, data, new TypeToken<ApiResponse<String>>() {
        }.getType(), new ActionCallbackListener<String>() {
            @Override
            public void onSuccess(String message, String data) {
                // 获取到 editFeidiNumber的值并上传  设置已綁定邀請人的标志
                Toast.makeText(getContext(), "绑定成功", Toast.LENGTH_SHORT).show();
                UserManager.setWaterDelverId(data);
                getActivity().finish();
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                switch (errorEvent) {
                    case ErrorEvent.UNREGISTER_WATER_DELIVER:
                        //飞滴号格式错误或无法更改
                        makeToast("该账号不存在");
                        break;
                    default:
                        makeToast("该账号不存在");
                        break;
                }
            }
        }));
    }
}
